package com.share.example.selenium.service.customer.gefera;

import com.share.common.utils.DelayUtil;
import com.share.example.selenium.service.AbsDriver;
import com.share.example.selenium.utils.DriverUtil;
import lombok.Data;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.StringUtils;
import org.openqa.selenium.By;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.WebElement;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.util.CollectionUtils;

import java.util.LinkedList;
import java.util.List;

@Data
@Slf4j
@Service
public class CustomerGeferaService extends AbsDriver {
    private final String BASE_URL = "https://online.gefera.ru/";
    private final String GEFERA_FIRST_PAGE_URL = "https://online.gefera.ru/eng/info/IL24/catalog/";

    @Autowired
    protected WebDriver driver;

    @Override
    public void reInitDriver(WebDriver driver) {
        this.driver = driver;
    }

    //所有页数据分析
    public void analyseAllPage() {
        List<String> reDate = new LinkedList<>();
        LinkedList<String> waitDeal = new LinkedList<>();
        LinkedList<String> hadDeal = new LinkedList<>();
        waitDeal.add(GEFERA_FIRST_PAGE_URL);

        while (!waitDeal.isEmpty()) {
            String url = waitDeal.remove();
            hadDeal.add(url);

            //分析一页数据
            List<String> pageUrl = analyseOnePage(url, reDate);

            //心的url列表
            if (!CollectionUtils.isEmpty(pageUrl)) {
                for (String newUrl : pageUrl) {
                    if (!hadDeal.contains(newUrl)) {
                        waitDeal.add(newUrl);
                    }
                }
            }
        }
    }


    //一页数据分析
    public List<String> analyseOnePage(String onePageUrl, List<String> reData) {
        long startTime = System.currentTimeMillis();

        //1.打开门户网站首页
        log.info("正在打开网址={}", onePageUrl);
        driver.get(onePageUrl);
        DelayUtil.sleepRangMillis(800, 1500);

        //2.列表项分析
        log.info("查询当前页列表");
        List<String> detailHrefList = new LinkedList<>();
        WebElement pItem = driver.findElement(By.xpath("//*[@id=\"pjax-container\"]/div[2]/div[1]"));
        List<WebElement> lista = pItem.findElements(By.xpath("div[@class=\"info-card hoverable\"]/a"));
        log.info("详情列表中需要分析的项数={}", lista.size());
        for (WebElement item : lista) {
            String href = item.getAttribute("href");
            String a = BASE_URL + href;
            log.info("link={}", a);
            detailHrefList.add(a);
        }

        //3.访问详情
        for (String href : detailHrefList) {
            log.info("正在打开详情页url={}", onePageUrl);
            String s = analyseDetail(href);
            reData.add(s);
        }

        //4.翻页系数
        List<String> pageHrefList = new LinkedList<>();
        WebElement paginationItem = driver.findElement(By.xpath("//*[@id=\"pjax-container\"]/div[2]/div[1]/ul"));
        List<WebElement> listlia = paginationItem.findElements(By.xpath("li/a"));
        for (WebElement item : listlia) {
            String href = item.getAttribute("href");
            if (StringUtils.isEmpty(href) || href.equals("#")) {
                continue;
            }
            String a = BASE_URL + href;
            log.info("pagelink={}", a);
            pageHrefList.add(a);
        }

        return pageHrefList;
    }

    //详情页数据提取
    public String analyseDetail(String detailUrl) {
        driver.get(detailUrl);
        DelayUtil.sleepRangMillis(800, 1500);
        String text = driver.findElement(By.xpath("/html/body/main/div[2]/div[2]/div/div[3]")).getText();
        log.info("");
        log.info("{}", text);
        return text;
    }

    public static void main(String[] args) {
        WebDriver driver = DriverUtil.getDriver("fdfdfd");
        CustomerGeferaService al = new CustomerGeferaService();
        al.reInitDriver(driver);

//        al.analyseDetail("https://online.gefera.ru/eng/info/IL24/catalog/09905268-5408-e715-f1a3-fcec66798217/");

        al.analyseAllPage();
    }

}
